﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MForm;
using ORMapping;
using BLL;
using PublicCommon;
using Enums;

namespace ModifyDataMoudle.Integration
{
    public partial class IntegrationManageForm : MdiForm
    {
        private TBLCardTypeBLL tctb = new TBLCardTypeBLL();
        private EnumIntegrationManageState CurrentlyState = EnumIntegrationManageState.QUERY_WAIT; 
        public IntegrationManageForm(Parent im): base(im)
        {
            InitializeComponent();
            IList<TBLCardType> ilSource = tctb.GetAllCardTypes();
            ilSource.Insert(0, new TBLCardType { CardType = string.Empty, CardName = string.Empty });
            TBLCardType_CardType.DataSource = ilSource;
            Card_Valid.DataSource = YNStatesInstance.DataSource;
            MemberInfo_Sex.DataSource = SexStatesInstance.DataSource;
            //可快捷查询的控件
            base.ShortcutQueryControlName.Add(Card_CardNO);
            base.ShortcutQueryControlName.Add(MemberInfo_MemberName);
        }

        public override void Query()
        {
            //根据焦点判断是通过那一个条件进行查询
            if (Card_CardNO.Focused)
            {
                QueryByCardNO();
                return;
            }

            if (MemberInfo_MemberName.Focused)
            {
                QueryByMemberInfo();
                return;
            }
        
        }

        private void QueryAfter(ref Control.ControlCollection cc)
        {
            foreach (Control c in cc)
            {
                if (c.Controls.Count > 0)
                {
                    Control.ControlCollection ccc = c.Controls;
                    QueryAfter(ref ccc);
                    continue;
                }

                if (0 == c.TabIndex)
                {
                    c.Focus();
                    TextBox TextBoxTemp = c as TextBox;
                    if (TextBoxTemp != null)
                    {
                        TextBoxTemp.SelectionStart = 0;
                        TextBoxTemp.SelectionLength = TextBoxTemp.Text.Length;
                    }
                    return;
                }
            }
        }

        private void QueryByMemberInfo()
        {
            //只对MemberInfoGroup进行清空和查询赋值
            String MemberName = MemberInfo_MemberName.Text;
            Control.ControlCollection cc = MemberInfoGroup.Controls;
            base.Reset(ref cc);
             
            MemberInfoBLL mib = new MemberInfoBLL();
            IList<MemberInfo> lmi = mib.GetMemberInfoByName(MemberName);

            if (lmi == null)
            {
                lalCardAlter.Text = String.Format("{0}会员查询不到", MemberName);
                lalCardAlter.ForeColor = Color.Red;
                lalCardAlter.Visible = true;
                return;
            }

            //重名时需要选择
            //if (lmi.Count > 1)
            //{

            //}
            Functions.SetControlTextFromObject<MemberInfo>(this.Controls, lmi[0]);
            Functions.SetPictureBox(MemberInfo_Photo, lmi[0].GetPhoto());

           //根据会员名查询卡时需要刷新卡信息  
        }

        private void QueryByCardNO()
        {
            String CardNO = Card_CardNO.Text;
            if (!Reset())
            {
                return;
            }

            CurrentlyState = EnumIntegrationManageState.QUERY_WAIT;
            New.Enabled = true;
            try
            {
                CardBLL cdb = new CardBLL();
                Card cd = cdb.QueryCardByNO(CardNO);

                if (cd == null)
                {
                    lalCardAlter.Text = String.Format("查询不到{0}攀岩卡", CardNO);
                    lalCardAlter.ForeColor = Color.Red;
                    lalCardAlter.Visible = true;
                    return;
                }

                Functions.SetControlTextFromObject<Card>(this.Controls, cd);
                Functions.SetPictureBox(MemberInfo_Photo, cd.MemberID.GetPhoto());

                lalCardAlter.Text = String.Format("{0}卡有效", cd.CardNO);
                lalCardAlter.ForeColor = Color.Blue;

                if (Functions.HasCardWarning(cd.StartDate, cd.EndDate))
                {
                    TimeSpan Day = cd.EndDate - DateTime.Now.Date;
                    lalCardAlter.Text = String.Format("{0}卡还有{1}天就到期了", cd.CardNO, Day.TotalDays);
                    lalCardAlter.ForeColor = System.Drawing.Color.FromArgb(-2790912);
                    lalCardAlter.Visible = true;
                    return;
                }

                if (Functions.HasCardVaild(cd.StartDate, cd.EndDate))
                {
                    lalCardAlter.Text = String.Format("{0}卡已经过期", cd.CardNO);
                    lalCardAlter.ForeColor = Color.Red;
                }

                lalCardAlter.Visible = true;
            }
            finally
            {
                //定位到TAG为0的那个控件，并选中其所有输入内容
                Control.ControlCollection ccc = this.Controls;
                QueryAfter(ref ccc);
            }    
        }

        public override void Reset(ref Control.ControlCollection cc)
        {
            New.Enabled = true;
            lalCardAlter.Visible = false;
            base.Reset(ref cc);
        }

        private void Save_Click(object sender, EventArgs e)
        {
            CurrentlyState = EnumIntegrationManageState.SAVING;
            MForm.CheckInputValueVaild civv = new MForm.CheckInputValueVaild();
            String strLastError = String.Empty;
            if (!civv.CheckVaild(this.Name, this.Controls, ref strLastError))
            {
                MessageBox.Show(strLastError);
                return;
            }

            if (Card_StartDate.Value >= Card_EndDate.Value)
            {
                MessageBox.Show("有效期止必须大于有效期起");
                return;
            }
            
            IntegrationManageBLL ib = new IntegrationManageBLL();
            if (!ib.Add(this.Controls))
            {
                return;
            }
            CurrentlyState = EnumIntegrationManageState.QUERY_WAIT;
            //重提数据（得到新建数据的ID）
            Card_CardNO.Focus();
            Query();
            MessageBox.Show("信息保存成功");
        }

        private void ReSetInto_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private bool Reset()
        {
            if (!IsReset())
            {
                return false;
            }
            Control.ControlCollection ccc = this.Controls;
            Reset(ref ccc);
            return true;
        }

        private void btnOpenPhoto_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK != ofdPhoto.ShowDialog())
            {
                return;
            }
            Bitmap bmPhoto = new Bitmap(ofdPhoto.FileName);
            MemberInfo_Photo.Image = bmPhoto;
        }

        private void New_Click(object sender, EventArgs e)
        {
            CurrentlyState = Reset() ? EnumIntegrationManageState.NEW : CurrentlyState;
            New.Enabled = false;
        }

        private bool IsReset()
        {
            if (CurrentlyState != EnumIntegrationManageState.QUERY_WAIT)
            {
                //提示保存信息
                if (DialogResult.Yes == MessageBox.Show("当前信息已被修改过，是否保存修改过的信息", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                {
                    return false;
                }
                return true;
            }
            return true;
        }

        private void QueryInfo_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Card_CardNO.Text))
            {
                QueryByCardNO();
                return;
            }

            if (!String.IsNullOrEmpty(MemberInfo_MemberName.Text))
            {
                QueryByMemberInfo();
                return;
            }
        }
    }
}
